/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.dao.hibernate;

import com.minierp.bean.UserBean;
import com.minierp.dao.PurchaseBillDao;
import com.minierp.dao.hibernate.BaseDaoImpl;
import com.minierp.helper.EncCommonHelper;
import com.minierp.model.PurchaseBill;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

public class PurchaseBillDaoImpl extends BaseDaoImpl implements PurchaseBillDao {
	private String getBlearSearchHql(String searchKey) {
		String str = "";
		if (searchKey != null) {
			searchKey = searchKey.trim();
		}

		if (searchKey != null && !searchKey.equals("")) {
			str = " and ((b.billCode like :billCode)  or (e.estName like :estName) or (e.nameSpell like :nameSpell) or (b.content like :content))";
		}

		return str;
	}

	private void setBlearSearchValue(Query q, String searchKey) {
		if (searchKey != null) {
			searchKey = searchKey.trim();
		}

		if (searchKey != null && !searchKey.equals("")) {
			q.setString("billCode", "%" + searchKey + "%");
			q.setString("estName", "%" + searchKey + "%");
			q.setString("nameSpell", "%" + searchKey + "%");
			q.setString("content", "%" + searchKey + "%");
		}

	}

	public PurchaseBill get(String billCode) {
		String sql = "select b from com.minierp.model.PurchaseBill b,com.minierp.model.PurchaseBillExt be where b.id=be.id and b.billCode = :billCode and be.enabled=\'Y\' order by b.id";
		String[] paramNames = new String[]{"billCode"};
		Object[] values = new Object[]{billCode};
		List list = this.getHibernateTemplate().findByNamedParam(sql,
				paramNames, values);
		return list.isEmpty() ? null : (PurchaseBill) list.get(0);
	}

	public List<?> getList(String type, String searchKey, boolean examined,
			long tsId, long estManagerId, UserBean ub, Date allowStart,
			int startIndex, int num) throws Exception {
		String str = this.getBlearSearchHql(searchKey);
		if (tsId != 0L) {
			str = str + " and b.tallySetId=" + tsId;
		} else {
			str = str
					+ EncCommonHelper.createTallySetSQLStr("b.tallySetId", ub,
							true);
		}

		if (estManagerId != 0L) {
			str = str + " and e.managerId=" + estManagerId;
		}

		if (allowStart != null) {
			str = str + " and b.purchaseDate>=:allowStart";
		}

		String examineFlag = "N";
		if (examined) {
			examineFlag = "Y";
		}

		String sql = "select b,e.estName from com.minierp.model.PurchaseBill b,com.minierp.model.PurchaseBillExt be,com.minierp.model.Establishment e where b.id=be.id and b.providerId=e.id and be.examined=:examined and b.billType=:billType and be.enabled=\'Y\' "
				+ str + " order by b.id desc";
		Session sess = null;

		List arg17;
		try {
			sess = this.getSession();
			Query q = sess.createQuery(sql);
			this.setBlearSearchValue(q, searchKey);
			if (allowStart != null) {
				q.setDate("allowStart", allowStart);
			}

			q.setString("examined", examineFlag);
			q.setString("billType", type);
			q.setFirstResult(startIndex);
			q.setMaxResults(num);
			arg17 = q.list();
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return arg17;
	}

	public int getListCount(String type, String searchKey, boolean examined,
			long tsId, long estManagerId, UserBean ub, Date allowStart)
			throws Exception {
		String str = this.getBlearSearchHql(searchKey);
		if (tsId != 0L) {
			str = str + " and b.tallySetId=" + tsId;
		} else {
			str = str
					+ EncCommonHelper.createTallySetSQLStr("b.tallySetId", ub,
							true);
		}

		if (estManagerId != 0L) {
			str = str + " and e.managerId=" + estManagerId;
		}

		if (allowStart != null) {
			str = str + " and b.purchaseDate>=:allowStart";
		}

		String examineFlag = "N";
		if (examined) {
			examineFlag = "Y";
		}

		String sql = "select count(b.id) from com.minierp.model.PurchaseBill b,com.minierp.model.PurchaseBillExt be,com.minierp.model.Establishment e where b.id=be.id and b.providerId=e.id and be.examined=:examined and b.billType=:billType and be.enabled=\'Y\' "
				+ str;
		Session sess = null;

		try {
			sess = this.getSession();
			Query q = sess.createQuery(sql);
			this.setBlearSearchValue(q, searchKey);
			if (allowStart != null) {
				q.setDate("allowStart", allowStart);
			}

			q.setString("examined", examineFlag);
			q.setString("billType", type);
			List list = q.list();
			if (!list.isEmpty()) {
				int arg16 = Integer.parseInt(list.get(0).toString());
				return arg16;
			}
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return 0;
	}

	public List<?> getListForCancel(String type, String searchKey, long tsId,
			long estManagerId, UserBean ub, Date allowStart, int startIndex,
			int num) throws Exception {
		String str = this.getBlearSearchHql(searchKey);
		if (tsId != 0L) {
			str = str + " and b.tallySetId=" + tsId;
		} else {
			str = str
					+ EncCommonHelper.createTallySetSQLStr("b.tallySetId", ub,
							true);
		}

		if (estManagerId != 0L) {
			str = str + " and e.managerId=" + estManagerId;
		}

		if (allowStart != null) {
			str = str + " and b.purchaseDate>=:allowStart";
		}

		String sql = "select b,e.estName,be.examined from com.minierp.model.PurchaseBill b,com.minierp.model.PurchaseBillExt be,com.minierp.model.Establishment e where b.id=be.id and b.providerId=e.id and be.enabled=\'Y\' "
				+ str + " and b.billType=:billType" + " order by b.id desc";
		Session sess = null;

		List arg15;
		try {
			sess = this.getSession();
			Query q = sess.createQuery(sql);
			this.setBlearSearchValue(q, searchKey);
			if (allowStart != null) {
				q.setDate("allowStart", allowStart);
			}

			q.setString("billType", type);
			q.setFirstResult(startIndex);
			q.setMaxResults(num);
			arg15 = q.list();
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return arg15;
	}

	public int getListForCancelCount(String type, String searchKey, long tsId,
			long estManagerId, UserBean ub, Date allowStart) throws Exception {
		String str = this.getBlearSearchHql(searchKey);
		if (tsId != 0L) {
			str = str + " and b.tallySetId=" + tsId;
		} else {
			str = str
					+ EncCommonHelper.createTallySetSQLStr("b.tallySetId", ub,
							true);
		}

		if (estManagerId != 0L) {
			str = str + " and e.managerId=" + estManagerId;
		}

		if (allowStart != null) {
			str = str + " and b.purchaseDate>=:allowStart";
		}

		String sql = "select count(b.id) from com.minierp.model.PurchaseBill b,com.minierp.model.PurchaseBillExt be,com.minierp.model.Establishment e where b.id=be.id and b.providerId=e.id and be.enabled=\'Y\' "
				+ str + " and b.billType=:billType";
		Session sess = null;

		try {
			sess = this.getSession();
			Query q = sess.createQuery(sql);
			this.setBlearSearchValue(q, searchKey);
			if (allowStart != null) {
				q.setDate("allowStart", allowStart);
			}

			q.setString("billType", type);
			List list = q.list();
			if (!list.isEmpty()) {
				int arg14 = Integer.parseInt(list.get(0).toString());
				return arg14;
			}
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return 0;
	}

	public Object[] getStatusValues(long id) {
		String sql = "select b.updateTime,b.providerId from com.minierp.model.PurchaseBill b where b.id="
				+ id;
		List list = this.getHibernateTemplate().find(sql);
		return list.size() > 0 ? (Object[]) list.get(0) : null;
	}

	public List<?> getListForPay(long providerId, String billType,
			long tallySetId, Date startDate, Date endDate) {
		String sql = "select b,be from com.minierp.model.PurchaseBill b,com.minierp.model.PurchaseBillExt be where b.id=be.id and b.providerId="
				+ providerId
				+ " and be.enabled=\'Y\'"
				+ " and be.examined=\'Y\'"
				+ " and be.paied=\'N\'"
				+ " and b.tallySetId=" + tallySetId;
		if (billType != null && !billType.equals("")) {
			sql = sql + " and b.billType=:billType";
		}

		if (startDate != null) {
			sql = sql + " and b.purchaseDate>=:startDate";
		}

		if (endDate != null) {
			sql = sql + " and b.purchaseDate<:endDate";
		}

		sql = sql + " order by b.purchaseDate";
		Session sess = null;

		List arg11;
		try {
			sess = this.getSession();
			Query q = sess.createQuery(sql);
			if (billType != null && !billType.equals("")) {
				q.setString("billType", billType);
			}

			if (startDate != null) {
				q.setDate("startDate", startDate);
			}

			if (endDate != null) {
				q.setDate("endDate", endDate);
			}

			arg11 = q.list();
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return arg11;
	}

	public List<?> getList(long providerId, boolean examined, UserBean ub) {
		String examineFlag = "N";
		if (examined) {
			examineFlag = "Y";
		}

		String sql = "select b from com.minierp.model.PurchaseBill b,com.minierp.model.PurchaseBillExt be where b.id=be.id and be.enabled=\'Y\' and be.examined=\'"
				+ examineFlag
				+ "\'"
				+ EncCommonHelper
						.createTallySetSQLStr("b.tallySetId", ub, true)
				+ " order by b.purchaseDate desc,b.billCode desc";
		return this.getHibernateTemplate().find(sql);
	}

	public List<?> getListForPay(String searchKey, Date startDate,
			Date endDate, String billCode, String minCost, String maxCost,
			long tsId, long estManagerId, UserBean ub, int startIndex, int num)
			throws Exception {
		String str = "";
		if (searchKey != null) {
			searchKey = searchKey.trim();
		}

		if (searchKey != null && !searchKey.equals("")) {
			str = " and ((e.estCode like :estCode)  or (e.estName like :estName))";
		}

		if (billCode != null) {
			billCode = billCode.trim();
		}

		if (billCode != null && !billCode.equals("")) {
			str = str + " and (b.billCode like :billCode)";
		}

		if (minCost != null && !minCost.equals("")) {
			str = str + " and (b.cost-be.paiedCost+be.adjustCost)>=" + minCost;
		}

		if (maxCost != null && !maxCost.equals("")) {
			str = str + " and (b.cost-be.paiedCost+be.adjustCost)<" + maxCost;
		}

		if (startDate != null) {
			str = str + " and b.purchaseDate>=:startDate";
		}

		if (endDate != null) {
			str = str + " and b.purchaseDate<:endDate";
		}

		if (tsId != 0L) {
			str = str + " and b.tallySetId=" + tsId;
		} else {
			str = str
					+ EncCommonHelper.createTallySetSQLStr("b.tallySetId", ub,
							true);
		}

		if (estManagerId != 0L) {
			str = str + " and e.managerId=" + estManagerId;
		}

		String sql = "select b,e.estName,be.paiedCost,be.adjustCost from com.minierp.model.PurchaseBill b,com.minierp.model.PurchaseBillExt be,com.minierp.model.Establishment e where b.id=be.id and b.providerId=e.id"
				+ str
				+ " and be.enabled=\'Y\'"
				+ " and be.examined=\'Y\'"
				+ " and be.paied=\'N\'" + " order by b.purchaseDate desc";
		Session sess = null;

		List arg18;
		try {
			sess = this.getSession();
			Query q = sess.createQuery(sql);
			if (searchKey != null && !searchKey.equals("")) {
				q.setString("estCode", "%" + searchKey + "%");
				q.setString("estName", "%" + searchKey + "%");
			}

			if (startDate != null) {
				q.setDate("startDate", startDate);
			}

			if (endDate != null) {
				q.setDate("endDate", endDate);
			}

			if (billCode != null && !billCode.equals("")) {
				q.setString("billCode", "%" + billCode + "%");
			}

			q.setFirstResult(startIndex);
			q.setMaxResults(num);
			arg18 = q.list();
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return arg18;
	}

	public List getListForPayCount(String searchKey, Date startDate,
			Date endDate, String billCode, String minCost, String maxCost,
			long tsId, long estManagerId, UserBean ub) throws Exception {
		String str = "";
		if (searchKey != null) {
			searchKey = searchKey.trim();
		}

		if (searchKey != null && !searchKey.equals("")) {
			str = " and ((e.estCode like :estCode)  or (e.estName like :estName))";
		}

		if (billCode != null) {
			billCode = billCode.trim();
		}

		if (billCode != null && !billCode.equals("")) {
			str = str + " and (b.billCode like :billCode)";
		}

		if (minCost != null && !minCost.equals("")) {
			str = str + " and (b.cost-be.paiedCost+be.adjustCost)>=" + minCost;
		}

		if (maxCost != null && !maxCost.equals("")) {
			str = str + " and (b.cost-be.paiedCost+be.adjustCost)<" + maxCost;
		}

		if (startDate != null) {
			str = str + " and b.purchaseDate>=:startDate";
		}

		if (endDate != null) {
			str = str + " and b.purchaseDate<:endDate";
		}

		if (tsId != 0L) {
			str = str + " and b.tallySetId=" + tsId;
		} else {
			str = str
					+ EncCommonHelper.createTallySetSQLStr("b.tallySetId", ub,
							true);
		}

		if (estManagerId != 0L) {
			str = str + " and e.managerId=" + estManagerId;
		}

		String sql = "select count(b.id),sum(b.cost),sum(be.paiedCost),sum(be.adjustCost),b.billType from com.minierp.model.PurchaseBill b,com.minierp.model.PurchaseBillExt be,com.minierp.model.Establishment e where b.id=be.id and b.providerId=e.id"
				+ str
				+ " and be.enabled=\'Y\'"
				+ " and be.examined=\'Y\'"
				+ " and be.paied=\'N\'" + " group by b.billType";
		Session sess = null;

		List arg16;
		try {
			sess = this.getSession();
			Query q = sess.createQuery(sql);
			if (searchKey != null && !searchKey.equals("")) {
				q.setString("estCode", "%" + searchKey + "%");
				q.setString("estName", "%" + searchKey + "%");
			}

			if (startDate != null) {
				q.setDate("startDate", startDate);
			}

			if (endDate != null) {
				q.setDate("endDate", endDate);
			}

			if (billCode != null && !billCode.equals("")) {
				q.setString("billCode", "%" + billCode + "%");
			}

			arg16 = q.list();
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return arg16;
	}

	public BigDecimal getSmsImprest(Date startDate, Date endDate,
			String billType) throws Exception {
		String sql = "select sum(b.imprest) from com.minierp.model.PurchaseBill b,com.minierp.model.PurchaseBillExt be where b.id=be.id and be.enabled=\'Y\' and be.examined=\'Y\' and b.purchaseDate>=:startDate and b.purchaseDate<:endDate and b.billType=:billType";
		Session sess = null;

		BigDecimal arg8;
		try {
			sess = this.getSession();
			Query q = sess.createQuery(sql);
			q.setDate("startDate", startDate);
			q.setDate("endDate", endDate);
			q.setString("billType", billType);
			List list = q.list();
			if (!list.isEmpty() && list.get(0) != null) {
				arg8 = new BigDecimal(list.get(0).toString());
				return arg8;
			}

			arg8 = new BigDecimal(0);
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return arg8;
	}

	public List<?> getMonthCost(Date startDate, Date endDate, long tsId,
			UserBean ub, long managerId, Date allowStart) throws Exception {
		String str = "";
		if (tsId != 0L) {
			str = str + " and b.tally_set_id=" + tsId;
		} else {
			str = str
					+ EncCommonHelper.createTallySetSQLStr("b.tally_set_id",
							ub, true);
		}

		if (managerId != 0L) {
			str = str + " and e.manager_id=" + managerId;
		}

		String sql = "select b.bill_type,month(b.purchase_date),sum(b.cost),sum(be.adjust_cost),sum(be.paied_cost) from purchase_bill b,purchase_bill_ext be ,establishment e where b.id=be.id and e.id=b.provider_id and be.enabled=\'Y\' and be.examined=\'Y\'"
				+ str
				+ " and b.purchase_date>=:startDate"
				+ " and b.purchase_date<:endDate"
				+ " group by month(b.purchase_date),b.bill_type";
		Session sess = null;

		List arg13;
		try {
			sess = this.getSession();
			SQLQuery q = sess.createSQLQuery(sql);
			if (allowStart != null && allowStart.after(startDate)) {
				q.setDate("startDate", allowStart);
			} else {
				q.setDate("startDate", startDate);
			}

			q.setDate("endDate", endDate);
			arg13 = q.list();
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return arg13;
	}

	public List<?> getListForAccountPay(long providerId, Date startDate,
			Date endDate, long tallySetId) {
		String sql = "select b,be from com.minierp.model.PurchaseBill b,com.minierp.model.PurchaseBillExt be where b.id=be.id and b.providerId="
				+ providerId
				+ " and be.enabled=\'Y\'"
				+ " and be.examined=\'Y\'"
				+ " and be.paied=\'N\'"
				+ " and b.tallySetId=" + tallySetId;
		if (startDate != null) {
			sql = sql + " and b.purchaseDate>=:startDate";
		}

		if (endDate != null) {
			sql = sql + " and b.purchaseDate<:endDate";
		}

		Session sess = null;

		List arg10;
		try {
			sess = this.getSession();
			Query q = sess.createQuery(sql);
			if (startDate != null) {
				q.setDate("startDate", startDate);
			}

			if (endDate != null) {
				q.setDate("endDate", endDate);
			}

			arg10 = q.list();
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return arg10;
	}

	public boolean isHasBill(long providerId) {
		String sql = "select b.id from purchase_bill b,purchase_bill_ext be where b.id=be.id and be.enabled=\'Y\' and b.provider_id="
				+ providerId + " limit 1";
		Session sess = null;

		try {
			sess = this.getSession();
			SQLQuery q = sess.createSQLQuery(sql);
			List list = q.list();
			if (list.size() <= 0) {
				return false;
			}
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return true;
	}

	public int estCorporate(final long fromClientId, final long toClientId) {
		Object obj = this.getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						String hql = "update com.minierp.model.PurchaseBill set providerId="
								+ toClientId
								+ " ,updateTime=:updateTime"
								+ " where providerId=" + fromClientId;
						Query q = session.createQuery(hql);
						q.setTimestamp("updateTime", new Date());
						return new Integer(q.executeUpdate());
					}
				});
		return ((Integer) obj).intValue();
	}

	public List getBaImprest(long tsId, UserBean ub, Date startDate,
			Date endDate) throws Exception {
		String str = "";
		if (tsId != 0L) {
			str = str + " and b.tallySetId=" + tsId;
		} else {
			str = str
					+ EncCommonHelper.createTallySetSQLStr("b.tallySetId", ub,
							true);
		}

		String sql = "select sum(b.imprest),b.bankAccountId,b.billType from com.minierp.model.PurchaseBill b,com.minierp.model.PurchaseBillExt be where b.id=be.id and be.enabled=\'Y\' and be.examined=\'Y\' and b.purchaseDate>=:startDate and b.purchaseDate<:endDate"
				+ str + " group by b.bankAccountId,b.billType";
		Session sess = null;

		List arg10;
		try {
			sess = this.getSession();
			Query q = sess.createQuery(sql);
			q.setDate("startDate", startDate);
			q.setDate("endDate", endDate);
			arg10 = q.list();
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return arg10;
	}

	public List<?> getListForStoc(String type, String searchKey, boolean stoc,
			long tsId, long estManagerId, UserBean ub, Date allowStart,
			int startIndex, int num) throws Exception {
		String str = this.getBlearSearchHql(searchKey);
		if (tsId != 0L) {
			str = str + " and b.tallySetId=" + tsId;
		} else {
			str = str
					+ EncCommonHelper.createTallySetSQLStr("b.tallySetId", ub,
							true);
		}

		if (estManagerId != 0L) {
			str = str + " and e.managerId=" + estManagerId;
		}

		if (allowStart != null) {
			str = str + " and b.purchaseDate>=:allowStart";
		}

		String stocFlag = "N";
		if (stoc) {
			stocFlag = "Y";
		}

		String sql = "select b,e.estName from com.minierp.model.PurchaseBill b,com.minierp.model.PurchaseBillExt be,com.minierp.model.Establishment e where b.id=be.id and b.providerId=e.id and be.stoc=\'"
				+ stocFlag
				+ "\'"
				+ " and b.billType=:billType"
				+ " and be.examined=\'Y\'"
				+ " and be.enabled=\'Y\'"
				+ str
				+ " order by b.id desc";
		Session sess = null;

		List arg17;
		try {
			sess = this.getSession();
			Query q = sess.createQuery(sql);
			this.setBlearSearchValue(q, searchKey);
			if (allowStart != null) {
				q.setDate("allowStart", allowStart);
			}

			q.setString("billType", type);
			q.setFirstResult(startIndex);
			q.setMaxResults(num);
			arg17 = q.list();
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return arg17;
	}

	public int getListForStocCount(String type, String searchKey, boolean stoc,
			long tsId, long estManagerId, UserBean ub, Date allowStart)
			throws Exception {
		String str = this.getBlearSearchHql(searchKey);
		if (tsId != 0L) {
			str = str + " and b.tallySetId=" + tsId;
		} else {
			str = str
					+ EncCommonHelper.createTallySetSQLStr("b.tallySetId", ub,
							true);
		}

		if (estManagerId != 0L) {
			str = str + " and e.managerId=" + estManagerId;
		}

		if (allowStart != null) {
			str = str + " and b.purchaseDate>=:allowStart";
		}

		String stocFlag = "N";
		if (stoc) {
			stocFlag = "Y";
		}

		String sql = "select count(b.id) from com.minierp.model.PurchaseBill b,com.minierp.model.PurchaseBillExt be,com.minierp.model.Establishment e where b.id=be.id and b.providerId=e.id and be.stoc=\'"
				+ stocFlag
				+ "\'"
				+ " and b.billType=:billType"
				+ " and be.examined=\'Y\'" + " and be.enabled=\'Y\' " + str;
		Session sess = null;

		int arg16;
		try {
			sess = this.getSession();
			Query q = sess.createQuery(sql);
			this.setBlearSearchValue(q, searchKey);
			if (allowStart != null) {
				q.setDate("allowStart", allowStart);
			}

			q.setString("billType", type);
			List list = q.list();
			if (list.isEmpty()) {
				return 0;
			}

			arg16 = Integer.parseInt(list.get(0).toString());
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return arg16;
	}

	public List getAppListCount(Date startDate, Date endDate, String estName,
			String billType, String exam, String paie, long tsId, UserBean ub,
			long mngId) throws Exception {
		String str = "";
		if (startDate != null) {
			str = str + " and b.purchaseDate>=:startDate";
		}

		if (endDate != null) {
			str = str + " and b.purchaseDate<:endDate";
		}

		if (estName != null && !estName.equals("")) {
			str = str + " and (e.estName like :estName)";
		}

		if (billType != null && !billType.equals("")) {
			str = str + " and (b.billType=:billType)";
		}

		if (exam != null && !exam.equals("")) {
			str = str + " and be.examined=:exam";
		}

		if (paie != null && !paie.equals("")) {
			str = str + " and be.paied=:paie";
		}

		if (mngId != 0L) {
			str = str + " and e.managerId=" + mngId;
		}

		if (tsId != 0L) {
			str = str + " and b.tallySetId=" + tsId;
		} else {
			str = str
					+ EncCommonHelper.createTallySetSQLStr("b.tallySetId", ub,
							true);
		}

		String sql = "select count(*),sum(b.cost),sum(b.cost-be.paiedCost+be.adjustCost),b.billType from com.minierp.model.PurchaseBill b,com.minierp.model.PurchaseBillExt be,com.minierp.model.Establishment e where b.id=be.id and b.providerId=e.id"
				+ str + " and be.enabled=\'Y\'" + " group by b.billType";
		Session sess = null;

		List arg16;
		try {
			sess = this.getSession();
			Query q = sess.createQuery(sql);
			if (startDate != null) {
				q.setDate("startDate", startDate);
			}

			if (endDate != null) {
				q.setDate("endDate", endDate);
			}

			if (estName != null && !estName.equals("")) {
				q.setString("estName", "%" + estName + "%");
			}

			if (billType != null && !billType.equals("")) {
				q.setString("billType", billType);
			}

			if (exam != null && !exam.equals("")) {
				q.setString("exam", exam);
			}

			if (paie != null && !paie.equals("")) {
				q.setString("paie", paie);
			}

			arg16 = q.list();
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return arg16;
	}

	public List getAppList(Date startDate, Date endDate, String estName,
			String billType, String exam, String paie, long tsId, UserBean ub,
			long mngId, String orde, int startIndex, int num) throws Exception {
		String str = "";
		if (startDate != null) {
			str = str + " and b.purchaseDate>=:startDate";
		}

		if (endDate != null) {
			str = str + " and b.purchaseDate<:endDate";
		}

		if (estName != null && !estName.equals("")) {
			str = str + " and (e.estName like :estName)";
		}

		if (billType != null && !billType.equals("")) {
			str = str + " and (b.billType=:billType)";
		}

		if (exam != null && !exam.equals("")) {
			str = str + " and be.examined=:exam";
		}

		if (paie != null && !paie.equals("")) {
			str = str + " and be.paied=:paie";
		}

		if (mngId != 0L) {
			str = str + " and e.managerId=" + mngId;
		}

		if (tsId != 0L) {
			str = str + " and b.tallySetId=" + tsId;
		} else {
			str = str
					+ EncCommonHelper.createTallySetSQLStr("b.tallySetId", ub,
							true);
		}

		String ordeStr = " order by b.orderDate desc,b.billCode desc";
		if (orde != null) {
			if (orde.equals("PURC_DATE")) {
				ordeStr = " order by b.purchaseDate desc,b.billCode desc";
			} else if (orde.equals("PURC_COST")) {
				ordeStr = " order by b.cost desc,b.billCode desc";
			} else if (orde.equals("BILL_CODE")) {
				ordeStr = " order by b.billCode desc";
			} else if (orde.equals("ESTA_NAME")) {
				ordeStr = " order by e.nameSpell desc,b.billCode desc";
			}
		}

		String sql = "select b,e.estName,be from com.minierp.model.PurchaseBill b,com.minierp.model.PurchaseBillExt be,com.minierp.model.Establishment e where b.id=be.id and b.providerId=e.id"
				+ str + " and be.enabled=\'Y\'" + ordeStr;
		Session sess = null;

		List arg20;
		try {
			sess = this.getSession();
			Query q = sess.createQuery(sql);
			if (startDate != null) {
				q.setDate("startDate", startDate);
			}

			if (endDate != null) {
				q.setDate("endDate", endDate);
			}

			if (estName != null && !estName.equals("")) {
				q.setString("estName", "%" + estName + "%");
			}

			if (billType != null && !billType.equals("")) {
				q.setString("billType", billType);
			}

			if (exam != null && !exam.equals("")) {
				q.setString("exam", exam);
			}

			if (paie != null && !paie.equals("")) {
				q.setString("paie", paie);
			}

			q.setFirstResult(startIndex);
			q.setMaxResults(num);
			arg20 = q.list();
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return arg20;
	}

	public List getAppListForExamCount(String billType, String sk, long tsId,
			UserBean ub, Date allowStart, long mngId) throws Exception {
		String str = this.getBlearSearchHql(sk);
		if (billType != null && !billType.equals("")) {
			str = str + " and (b.billType=:billType)";
		}

		if (mngId != 0L) {
			str = str + " and e.managerId=" + mngId;
		}

		if (allowStart != null) {
			str = str + " and b.purchaseDate>=:allowStart";
		}

		if (tsId != 0L) {
			str = str + " and b.tallySetId=" + tsId;
		} else {
			str = str
					+ EncCommonHelper.createTallySetSQLStr("b.tallySetId", ub,
							true);
		}

		String sql = "select count(*),sum(b.cost) from com.minierp.model.PurchaseBill b,com.minierp.model.PurchaseBillExt be,com.minierp.model.Establishment e where b.id=be.id and b.providerId=e.id"
				+ str + " and be.examined=\'N\'" + " and be.enabled=\'Y\'";
		Session sess = null;

		List arg13;
		try {
			sess = this.getSession();
			Query q = sess.createQuery(sql);
			this.setBlearSearchValue(q, sk);
			if (billType != null && !billType.equals("")) {
				q.setString("billType", billType);
			}

			if (allowStart != null) {
				q.setDate("allowStart", allowStart);
			}

			arg13 = q.list();
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return arg13;
	}

	public List getAppListForExam(String billType, String sk, long tsId,
			UserBean ub, Date allowStart, long mngId, int startIndex, int num)
			throws Exception {
		String str = this.getBlearSearchHql(sk);
		if (billType != null && !billType.equals("")) {
			str = str + " and (b.billType=:billType)";
		}

		if (mngId != 0L) {
			str = str + " and e.managerId=" + mngId;
		}

		if (allowStart != null) {
			str = str + " and b.purchaseDate>=:allowStart";
		}

		if (tsId != 0L) {
			str = str + " and b.tallySetId=" + tsId;
		} else {
			str = str
					+ EncCommonHelper.createTallySetSQLStr("b.tallySetId", ub,
							true);
		}

		String sql = "select b,e.estName from com.minierp.model.PurchaseBill b,com.minierp.model.PurchaseBillExt be,com.minierp.model.Establishment e where b.id=be.id and b.providerId=e.id"
				+ str
				+ " and be.examined=\'N\'"
				+ " and be.enabled=\'Y\'"
				+ " order by b.purchaseDate desc,b.billCode desc";
		Session sess = null;

		List arg15;
		try {
			sess = this.getSession();
			Query q = sess.createQuery(sql);
			this.setBlearSearchValue(q, sk);
			if (billType != null && !billType.equals("")) {
				q.setString("billType", billType);
			}

			if (allowStart != null) {
				q.setDate("allowStart", allowStart);
			}

			q.setFirstResult(startIndex);
			q.setMaxResults(num);
			arg15 = q.list();
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return arg15;
	}

	public int getAppListForUnExamCount(String billType, String sk, long tsId,
			UserBean ub, Date allowStart, long mngId) throws Exception {
		String str = this.getBlearSearchHql(sk);
		if (billType != null && !billType.equals("")) {
			str = str + " and (b.billType=:billType)";
		}

		if (mngId != 0L) {
			str = str + " and e.managerId=" + mngId;
		}

		if (allowStart != null) {
			str = str + " and b.purchaseDate>=:allowStart";
		}

		if (tsId != 0L) {
			str = str + " and b.tallySetId=" + tsId;
		} else {
			str = str
					+ EncCommonHelper.createTallySetSQLStr("b.tallySetId", ub,
							true);
		}

		String sql = "select count(*) from com.minierp.model.PurchaseBill b,com.minierp.model.PurchaseBillExt be,com.minierp.model.Establishment e where b.id=be.id and b.providerId=e.id"
				+ str + " and be.examined=\'Y\'" + " and be.enabled=\'Y\'";
		Session sess = null;

		try {
			sess = this.getSession();
			Query q = sess.createQuery(sql);
			this.setBlearSearchValue(q, sk);
			if (billType != null && !billType.equals("")) {
				q.setString("billType", billType);
			}

			if (allowStart != null) {
				q.setDate("allowStart", allowStart);
			}

			List list = q.list();
			if (!list.isEmpty() && list.get(0) != null) {
				int arg14 = Integer.parseInt(list.get(0).toString());
				return arg14;
			}
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return 0;
	}

	public List getAppListForUnExam(String billType, String sk, long tsId,
			UserBean ub, Date allowStart, long mngId, int startIndex, int num)
			throws Exception {
		String str = this.getBlearSearchHql(sk);
		if (billType != null && !billType.equals("")) {
			str = str + " and (b.billType=:billType)";
		}

		if (mngId != 0L) {
			str = str + " and e.managerId=" + mngId;
		}

		if (allowStart != null) {
			str = str + " and b.purchaseDate>=:allowStart";
		}

		if (tsId != 0L) {
			str = str + " and b.tallySetId=" + tsId;
		} else {
			str = str
					+ EncCommonHelper.createTallySetSQLStr("b.tallySetId", ub,
							true);
		}

		String sql = "select b,e.estName,be.examinerName,be.examineTime from com.minierp.model.PurchaseBill b,com.minierp.model.PurchaseBillExt be,com.minierp.model.Establishment e where b.id=be.id and b.providerId=e.id"
				+ str
				+ " and be.examined=\'Y\'"
				+ " and be.enabled=\'Y\'"
				+ " order by be.examineTime desc";
		Session sess = null;

		List arg15;
		try {
			sess = this.getSession();
			Query q = sess.createQuery(sql);
			this.setBlearSearchValue(q, sk);
			if (billType != null && !billType.equals("")) {
				q.setString("billType", billType);
			}

			if (allowStart != null) {
				q.setDate("allowStart", allowStart);
			}

			q.setFirstResult(startIndex);
			q.setMaxResults(num);
			arg15 = q.list();
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return arg15;
	}
}